using System;
using System.Collections.Generic;
using System.Web.Mvc;
using RanchBuddy.Core.Attributes;
using RanchBuddy.Core.DataAccess;
using RanchBuddy.Core.DataAccess.Impl;
using RanchBuddy.Core.Domain.Impl;
using RanchBuddy.Core.Services;
using RanchBuddy.Core.Services.Impl;
using StructureMap;

namespace RanchBuddy.Web.Controllers
{
    public class CompanyController : SmartController
    {
        public ActionResult AssociateAccount(int CompanyTypeID, int CompanyID)
        {
            ViewData["CompanyTypeID"] = CompanyTypeID;
            ViewData["CompanyID"] = CompanyID;
            List<CompanyAccountType> companyAccountTypes = ObjectFactory.GetInstance<ICompanyAccountTypeService>().GetCompanyAccountTypes(CompanyTypeID);
            ViewData["CompanyAccountTypes"] = companyAccountTypes;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AssociateAccount(int CompanyID, int CompanyTypeID, FormCollection collection)
        {
            int AssociationTypeID = Convert.ToInt32(collection.GetValue("AssociationType").AttemptedValue);
            
            ViewData["CompanyTypeID"] = CompanyTypeID;
            ViewData["CompanyID"] = CompanyID;
            return View();
        }

        public ActionResult Index()
        {
            if (ObjectFactory.GetInstance<IWebContext>().Account == null && ObjectFactory.GetInstance<IWebContext>().Zipcode == null)
                return RedirectToAction<HomeController>(c => c.WhatIsYourZipCode(""),new {url = "Company"});

            int zip = 0;

            //get zip from account in session
            if (ObjectFactory.GetInstance<IWebContext>().Account != null && !string.IsNullOrEmpty(ObjectFactory.GetInstance<IWebContext>().Account.Zipcode))
                zip = Convert.ToInt32(ObjectFactory.GetInstance<IWebContext>().Account.Zipcode);
            
            //get zip from zip in session
            if (zip == 0 && !string.IsNullOrEmpty(ObjectFactory.GetInstance<IWebContext>().Zipcode))
                zip = Convert.ToInt32(ObjectFactory.GetInstance<IWebContext>().Zipcode);

            //ViewData["companies"] = new CompanyService().GetCompanies(zip, 30, 1, 100);
            ViewData["zip"] = zip;

            MapPoint mp = ObjectFactory.GetInstance<IPostalCodeRepository>().GetLatLongByZip(Convert.ToInt32(zip));

            ViewData["MapPoint"] = mp;

            List<Company> companies = ObjectFactory.GetInstance<ICompanyService>().GetCompanies(zip, 30, 1, 100);

            return View(companies);
        }

        public ActionResult WhyCreateAnAccount()
        {
            return View();
        }

        public ActionResult Details(string id)
        {
            Company c = ObjectFactory.GetInstance<ICompanyService>().GetCompanyByName((id));
            CompanyType ct = ObjectFactory.GetInstance<ICompanyTypeRepository>().GetCompanyTypeByID((int)c.CompanyTypeID);
            ViewData["CompanyType"] = ct.Name;
            return View(c);
        }

        public ActionResult DetailsByID(int id)
        {
            Company c = ObjectFactory.GetInstance<ICompanyService>().GetCompanyByID(id);
            CompanyType ct = ObjectFactory.GetInstance<ICompanyTypeRepository>().GetCompanyTypeByID((int)c.CompanyTypeID);
            ViewData["CompanyType"] = ct.Name;
            return RedirectToAction<CompanyController>(controller => controller.Details(c.Name.Replace(" ", "-")), new { id = c.Name.Replace(" ", "-") });
        }

        [MustBeLoggedIn]
        public ActionResult Create()
        {
            ViewData["CompanyTypes"] = ObjectFactory.GetInstance<ICompanyTypeRepository>().GetCompanyTypes();
            return View(new Company());
        } 

        [MustBeLoggedIn]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            Company company = new Company();
            try
            {
                int CompanyTypeID = Convert.ToInt32(collection.GetValue("CompanyType").AttemptedValue);

                UpdateModel(company);
                company.CreatedByAccountID = ObjectFactory.GetInstance<IWebContext>().Account.AccountID;
                company.CompanyTypeID = CompanyTypeID;
                IRepositoryStatus rs = ObjectFactory.GetInstance<ICompanyRepository>().SaveCompany(company);

                if (rs.StatusType == Status.StatusTypes.Success)
                    return RedirectToAction<CompanyController>(c=>c.Index());
                else
                    throw new Exception(rs.GetMessages().ToString());
            }
            catch(Exception e)
            {
                ViewData["CompanyTypes"] = ObjectFactory.GetInstance<ICompanyTypeRepository>().GetCompanyTypes();
                return View(company);
            }
        }

        [MustBeLoggedIn]
        public ActionResult Edit(int id)
        {
            if(id != 0)
            {
                ViewData["CompanyTypes"] = ObjectFactory.GetInstance<ICompanyTypeRepository>().GetCompanyTypes();
                IRepositoryStatus rs = ObjectFactory.GetInstance<ICompanyRepository>().GetCompanyByID(id);
                ViewData["TimeStamp"] = rs.GetObject<Company>().Timestamp.ToString();
                if (rs.StatusType == Status.StatusTypes.Success)
                    return View(rs.GetObject<Company>());
            }
            return View();
        }

        [MustBeLoggedIn]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection, string _timestamp)
        {
            if(id != 0)
            {
                Company company = new Company();
                UpdateModel(company);
                int CompanyTypeID = Convert.ToInt32(collection.GetValue("CompanyType").AttemptedValue);
                company.Timestamp = _timestamp.ToTimestampBinary();
                company.CompanyTypeID = CompanyTypeID;
                company.CompanyID = id;
                ViewData["CompanyTypes"] = ObjectFactory.GetInstance<ICompanyTypeRepository>().GetCompanyTypes();
                new CompanyRepository().SaveCompany(company);

                return RedirectToAction<CompanyController>(c => c.DetailsByID(id),new {id});
            }
            return View();
        }
    }
}
